/*
* @Author: Marte
* @Date:   2020-07-08 10:04:21
* @Last Modified by:   Marte
* @Last Modified time: 2020-07-31 16:47:51
*/

'use strict';
import Vue from 'vue'
import router from '@/router'
import { Toast } from "vant";
import { ImagePreview } from "vant";
import User from "@/service/my.js";
export default {
    ossUrl: 'https://jksy.oss-cn-shenzhen.aliyuncs.com',
	oldUid: '', //uid
    fnMsg(msg) {
        Toast({message:msg,
          position:'bottom'});
    },
    fnOpenCommon(ts, route) {
      let name = route.name, params = route.params, query = route.query;
        ts.$router.push({
          name: name,//这个name就是你刚刚配置在router里边的name
          params:params,
          query: query
        })
    },
    fnPreviewPic: function(path) {
      ImagePreview({
		showIndex: false,
        images: path
      });
    },
	// 图片压缩
	fnChangeFile:function (file, param, callback) {
		Toast.loading({
		  duration: 0, // 持续展示 toast
		  forbidClick: true,
		  loadingType: "spinner",
		  className: "loading-icon",
		  message: "识别中..."
		});
		let canvas = document.createElement('canvas') // 创建Canvas对象(画布)
		let context = canvas.getContext('2d')
		let img = new Image()
		img.src = file.content // 指定图片的DataURL(图片的base64编码数据)
		img.onload = () => {
		    canvas.width = param.w
		    canvas.height = param.h
		    context.drawImage(img, 0, 0, param.w, param.h)
		    file.content = canvas.toDataURL(file.file.type, 0.7)
		    let files = this.dataURLtoFile(file.content, file.file.name)
			let _file = {
			  file: files,
			  content: file.content
			}
			Toast.clear();
			if (_file) callback && callback(_file)
		}
	}, 
	// 将base64转换为file文件
	dataURLtoFile: function(dataurl, filename) {
		let arr = dataurl.split(',')
		let mime = arr[0].match(/:(.*?);/)[1]
		let bstr = atob(arr[1])
		let n = bstr.length
		let u8arr = new Uint8Array(n)
		while (n--) {
			u8arr[n] = bstr.charCodeAt(n)
		}
		return new File([u8arr], filename, {
			type: mime
		})
	},
	getScrollTop() {
		var scrollTop = window.pageYOffset  //用于FF
		|| document.documentElement.scrollTop  
		|| document.body.scrollTop  
		|| 0;
		return scrollTop            
	},
	handleScroll () {
		let screenH = document.body.clientHeight
		let windowScrollY = this.getScrollTop()  
		let disabled = windowScrollY > 0 ? true : false
		return disabled
	},
	//   打开客服聊天窗口
	openKefu () {
		window.qimoChatClick()
	},
    // 引入七陌机器人
    getQimo(uid) {
      if (!uid) {
        this.initQimo({
            nickName: "尊敬的试客"
          });
      } else {
        this.initQimo({
			userId: uid,
			nickName: `试客${uid}`
		  });
      }

      // console.log('输出结果信息', data)
    },
    initQimo(obj) {
		let vm = this;
      (function (a, h, c, b, f, g) {
        var nosignScript = document.querySelector("#qimo");
        // 存在链接就不再新生成七陌机器人
		 let {userId} = obj;
        if (nosignScript && vm.oldUid == userId ) {
          return;
        } else {
			vm.oldUid = userId
		}
        // 添加参数、必须添加到qimoClientId对象里面
		a['qimoClientId'] = obj;
        // 生成script节点
        g = h.createElement(c);
        g.async = 1;
        g.charset = "utf-8";
        g.src = b;
        g.setAttribute("id", "qimo");
        c = h.getElementsByTagName(c)[0];
        // 插入生成的script标签链接
        c.parentNode.insertBefore(g, c);
      })(
        window,
        document,
        "script",
        "https://ykf-webchat.7moor.com/javascripts/7moorInit.js?accessId=a60ba080-bc78-11ec-89f0-dd3b96c688de&autoShow=false&language=ZHCN",
        "ud"
      );
    }
}
